<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card>
        <el-button
          type="primary"
          size="small"
          icon="el-icon-plus"
          style="margin-bottom:20px"
          @click="dialogVisible = true"
        >
          新增角色
        </el-button>
        <el-table
          :data="list"
          border
          stripe
          style="width: 100%"
        >
          <el-table-column
            type="index"
            align="center"
            label="序号"
            width="180"
          />
          <el-table-column
            prop="name"
            align="center"
            label="名称"
            width="180"
          />
          <el-table-column
            prop="description"
            align="center"
            label="描述"
          />
          <el-table-column
            align="center"
            label="操作"
          >
            <template #default="{ row }">
              <el-button type="success" size="small">分配权限</el-button>
              <el-button type="primary" size="small" @click="showDialog(row.id)">编辑</el-button>
              <el-button type="danger" size="small" @click="deleteRole(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <el-row type="flex" justify="end" style="margin-top: 15px">
          <el-pagination
            :current-page="params.page"
            :page-size="params.pagesize"
            layout="prev, pager, next"
            :total="total"
            @current-change="handleCurrentChange"
          />
        </el-row>
        <el-dialog
          :title="roleForm.id?'编辑角色':'新增角色'"
          :visible="dialogVisible"
          width="30%"
          @close="closeDialog"
        >
          <el-form ref="roleRef" :model="roleForm" :rules="roleRules" label-width="90px">
            <el-form-item label="角色名称" prop="name">
              <el-input v-model="roleForm.name" />
            </el-form-item>
            <el-form-item label="角色描述" prop="description">
              <el-input v-model="roleForm.description" />
            </el-form-item>
          </el-form>
          <span slot="footer" class="dialog-footer">
            <el-button @click="dialogVisible = false">取 消</el-button>
            <el-button type="primary" @click="submit">确 定</el-button>
          </span>
        </el-dialog>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getRolesList, addRole, getRoleDetail, updateRole, deleteRole } from '@/api/setting'
export default {
  data() {
    return {
      list: [],
      params: {
        page: 1,
        pagesize: 2
      },
      total: 0,
      dialogVisible: false,
      roleForm: {
        name: '',
        description: ''
      },
      roleRules: {
        name: [
          { required: true, message: '角色名称不能为空', tirgger: ['blur'] }
        ],
        description: [
          { required: true, message: '角色描述不能为空', tirgger: ['blur'] }
        ]
      }
    }
  },
  created() {
    this.initData()
  },
  methods: {
    async initData() {
      const res = await getRolesList(this.params)
      this.list = res.data.rows
      this.total = res.data.total
    },
    handleCurrentChange(val) {
      this.params.page = val
      this.initData()
    },
    closeDialog() {
      this.dialogVisible = false
      this.$refs.roleRef.resetFields()
      this.roleForm = {
        name: '',
        description: ''
      }
    },
    submit() {
      this.$refs.roleRef.validate(async valid => {
        if (!valid) return

        if (this.roleForm.id) {
          await updateRole(this.roleForm)
        } else {
          await addRole(this.roleForm)
        }
        const msg = this.roleForm.id ? '编辑角色成功' : '新增角色成功'
        this.$message.success(msg)
        this.dialogVisible = false
        this.initData()
      })
    },
    async showDialog(id) {
      this.dialogVisible = true
      const res = await getRoleDetail(id)
      this.roleForm = res.data
    },
    deleteRole(id) {
      this.$confirm('此操作将永久删除该角色, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(async() => {
        await deleteRole(id)
        this.$message({
          type: 'success',
          message: '删除成功!'
        })
        if (this.list.length === 1 && this.params.page > 1) {
          this.params.page -= 1
        }
        this.initData()
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        })
      })
    }
  }
}
</script>

<style>

</style>
